package top.itaso.app.ui.storage

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import top.itaso.app.R
import top.itaso.lib.widget.vo.Node
import top.itaso.app.ui.treelevel.adapter.NodeTreeAdapter
import top.itaso.lib.common.StorageVolumeUtils

/**
 * @Description: 存储路径下的文件树管理界面：文件夹
 * @Author dengfu.su
 * @Date 2023/3/6
 */
class FolderTreeFragment : Fragment() {
    private lateinit var mViewModel: FolderTreeViewModel
    private lateinit var tvInfoDetail: TextView
    private lateinit var folderAdapter: NodeTreeAdapter
    private lateinit var rvFolderListView: RecyclerView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mViewModel = ViewModelProvider(this).get(FolderTreeViewModel::class.java)
        val rootView = inflater.inflate(R.layout.fragment_folder_tree, container, false)
        initView(rootView)
//        mViewModel.infoDetail.observe(viewLifecycleOwner, Observer {
//            tvInfoDetail.text = it
//        })
        mViewModel.folders.observe(viewLifecycleOwner, Observer {
            Log.d(TAG, "onCreateView: >>>>>>folders size=${it.size}")
            folderAdapter.refreshAllNode(it as List<Node<StorageVolumeUtils.UStorageVolume>>, 0);
        })
        return rootView
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
    }

    private fun initView(rootView: View) {
        tvInfoDetail = rootView.findViewById(R.id.tv_info_detail)
        rvFolderListView = rootView.findViewById(R.id.rv_folder)
        rvFolderListView.layoutManager = LinearLayoutManager(requireContext())
        folderAdapter = NodeTreeAdapter(
            mViewModel.folders.value as List<Node<StorageVolumeUtils.UStorageVolume>>,
            1
        )
        folderAdapter.setOnNodeTreeClickListener { node, position ->
            mViewModel.refreshChildFolder(node)
        }
//        folderAdapter.setOnNodeTreeClickListener(object {
//            override fun onClick(nodeTreeBean: Node<StorageVolumeUtils.UStorageVolume>, position: Int) {
//                mViewModel.refreshChildFolder(nodeTreeBean)
//            }
//
//        })
        rvFolderListView.adapter = folderAdapter
    }

    companion object {
        private const val TAG = "FolderTreeFragment"
    }

}